﻿using Minecraft.BLL.mysql;
using Minecraft.Config;
using Minecraft.Config.Csv.CsvCls;
using Minecraft.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Minecraft.ServerHall
{
	public static class FurnitureExt
	{
		/// <summary>
		/// 获取家具的配置信息
		/// </summary>
		/// <param name="furniture"></param>
		/// <returns></returns>
		public static Mod_Drawing GetMod_FurnitureInfo(this FurnitureModel furniture)
		{
			//return CsvConfig.furnitureList.FirstOrDefault(m => m.Id == furniture.FurnitureItemId);
			return CsvConfig.GetMod_DrawingInfoById(furniture.FurnitureItemId);
		}
		/// <summary>
		/// 各种类型家具信息初始化（from db，只能在玩家连接登录时调用一次）
		/// </summary>
		/// <param name="session"></param>
		public static void InitFurnitureListFromDb(this MinecraftSession session)
		{
			var furnitureList = FurnitureBLL.GetList(session.minecraftSessionInfo.player.PlayerId, out bool fromCache5);
			session.FurnitureClassify(furnitureList);
		}
		/// <summary>
		/// 家具分类存储
		/// </summary>
		/// <param name="session"></param>
		/// <param name="furnitureList"></param>
		public static void FurnitureClassify(this MinecraftSession session, List<FurnitureModel> furnitureList)
		{
			//家具集体操作
			//产出家具
			session.minecraftSessionInfo.furnitureOutputList = furnitureList.Where(m => m.enumFurnitureType == EnumFurnitureType.Output).ToList();
			//装饰家具
			session.minecraftSessionInfo.furnitureDecorateList = furnitureList.Where(m => m.enumFurnitureType == EnumFurnitureType.Decorate).ToList();
			//机关家具
			session.minecraftSessionInfo.furnitureMechanismList = furnitureList.Where(m => m.enumFurnitureType == EnumFurnitureType.Mechanism).ToList();
			//存储家具
			session.minecraftSessionInfo.furnitureStorageList = furnitureList.Where(m => m.enumFurnitureType == EnumFurnitureType.Storage).ToList();
			//宝箱
			session.minecraftSessionInfo.furnitureTreasureBoxList = furnitureList.Where(m => m.enumFurnitureType == EnumFurnitureType.Treasurebox).ToList();
		}
		/// <summary>
		/// 返回所有类型的家具
		/// </summary>
		/// <param name="session"></param>
		/// <returns></returns>
		public static List<FurnitureModel> GetAllFurnitureConvergeList(this MinecraftSession session, bool isContainingAllTreasureBox = false)
		{
			//家具集体操作
			var furnitureList = new List<FurnitureModel>();
			//产出家具
			furnitureList.AddRange(session.minecraftSessionInfo.furnitureOutputList);
			//装饰家具
			furnitureList.AddRange(session.minecraftSessionInfo.furnitureDecorateList);
			//机关家具
			furnitureList.AddRange(session.minecraftSessionInfo.furnitureMechanismList);
			//存储家具
			furnitureList.AddRange(session.minecraftSessionInfo.furnitureStorageList);
			if (isContainingAllTreasureBox)
			{
				//宝箱
				furnitureList.AddRange(session.minecraftSessionInfo.furnitureTreasureBoxList);
			}
			else
			{
				//宝箱
				furnitureList.AddRange(session.minecraftSessionInfo.furnitureTreasureBoxList.Where(m => m.TreasureBox_IsGenerate_Convert).ToList());
			}
			return furnitureList;
		}

		public static string GetNewFurnitureId(this MinecraftSession session)
		{
			if (!session.GetAllFurnitureConvergeList(true).Any())
			{
				return 1.ToString();
			}
			var maxId = session.GetAllFurnitureConvergeList(true).Max(m => m.FurnitureId_Convert);
			return (maxId + 1).ToString();
		}
	}
}
